use gri_secmgr qw(get_user_name);
use gri_userprefs;

sub mm_simple_portal_list($;$$) {
my $order=lc($_[0]);
my $all_portals=$_[1];
my $all_sections=$_[2];

	if(!defined($all_portals) || $all_portals eq "?all_portals - not defined?") {
		$all_portals="0";
	}
	if(!defined($all_sections) || $all_sections eq "?all_sections - not defined?") {
		$all_sections="0";
	}
	my $show_hovers=0;
	my $v=get_user_name();
	my $users_dir=$::GRI_FRONTEND->get_global_directory("users");
	if(-f "$users_dir/$v-prefs.xml") {
		my $xx=new gri_userprefs("$users_dir/$v-prefs.xml");
		if($xx) {
			my ($d,$d2);
			($d,$d2)=$xx->get_pref_value("hover_portal_info");
			$show_hovers=$d2 if(defined($d2));
		}
	}
	$show_hovers=to_bool($show_hovers);
	my $setup_init_portals=$::GRI_FRONTEND->get_variable("SETUP_INIT_PORTALS");
	if(!defined($setup_init_portals)) {
		$setup_init_portals=0;
	}

	# Get the username from the cookie

	my $request_access=0;
	my $lref=$::GRI_FRONTEND->get_portal_list();
	my @L=(@$lref);
	if(! @L && $setup_init_portals==1) {
		my ($a,$b)=$::GRI_FRONTEND->does_user_have_role(undef,'portal_admin');
		if($a==1) {
			my $l=link_to_str("stdlink","Setup","admin_page","");
			return '<div style="position: relative; top: 20px; width: 600px; margin: auto; background-color: red; color: white;"><br><div style="text-align: center;">No portals are yet defined - click ' . $l . ' to set one up.<br>&nbsp;</div></div>';
		}
		return '<div style="position: relative; top: 20px; width: 600px; margin: auto; background-color: red; color: white;"><br><div style="text-align: center;">No portals are yet defined - please contact your site administrator.<br>&nbsp;</div></div>';
	}
	if($order eq "rname") {
		@L=reverse(@L);
	}

	# We simply show the list of portals in a mini-table.

	if(! @L) {
		return "";
	}
	my $s="
<table class=gri_container cols=2>
<tr class=gri_headerrow>
  <td class=gri_cell>Available Portals</td>
  <td class=gri_cell-right>Actions</td>
</tr>";
	my $row=0; my $have_all=1;
	for my $c (@L) {
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		my $a=$::GRI_FRONTEND->is_user_allowed($c,$v);
		$n=string_to_hex($c);
		my $hs=""; my $ti=0; my $tr=0;
		if($a) {
			if($show_hovers) {
				my ($lref,$err)=$::GRI_FRONTEND->get_portal_report_names($c);
				if(defined($lref)) {
					for my $rep (@$lref) {
						my @info=$::GRI_FRONTEND->get_portal_report_details($c,$rep);
						if(defined($info[0]) && defined($info[5])) {
							$ti+=$info[5];
							$tr++;
						}
					}
					if($tr || $ti) {
						$hs="title=\"Reports: $tr\nReport Count: $ti\"";
					}
				}
			}

			$s.="<tr id=$class><td class=gri_cell $hs>$c</td>";
			my $l=link_to_str("stdlink","View","portal_tl","portal=$n");
			$s.="<td class=gri_cell-right>$l</td></tr>\n";
		} else {
			$have_all=0;
			if($all_portals eq "1") {
				if($v eq "public") {
					$s.="<tr id=$class><td class=gri_cell>$c</td>";
					$s.="<td class-gri_cell-right> </td></tr>\n";
				} else {
					my $n2=string_to_hex($v);
					my $portal=string_to_hex($c);
					my $l;
					$l=link_to_str("stdlink","New Request Access","form_portal_access_request","portal=$portal,user=$n2");
					$s.="<tr id=$class><td class=gri_cell>$c</td>";
					$s.="<td class=gri_cell-right>$l</td></tr>\n";
				}
			} else {
				$request_access=1;
				next;
			}
		}
		$row++;
	}
	# If the request_access is set then we issue a link to indicate viewing 
	# all links, otherwise turn off request access...
	if(! $have_all) {
		if($request_access) {
			my $class="gri_odd";
			$class="gri_even" if $row%2==0;
			my $l=link_to_str("stdlink","View all portals","_","all_portals=1,all_sections=$all_sections");
			$s.="<tr id=$class><td class=gri_cell-right colspan=2>$l</td></tr>";
		} else {
			my $class="gri_odd";
			$class="gri_even" if $row%2==0;
			my $l=link_to_str("stdlink","View accessible portals only","_","all_portals=0,all_sections=$all_sections");
			$s.="<tr id=$class><td class=gri_cell-right colspan=2>$l</td></tr>";
		}
	}
	return "$s</table>";
}

# vim: ts=4:sw=4:syntax=perl
